package cn.bulibuqi.myblog.controller;

import cn.bulibuqi.myblog.bean.Tag;
import cn.bulibuqi.myblog.service.TagService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

/**
 * @author bulibuqi
 * @create 2021-12-10:49
 */
@Controller
@RequestMapping("/admin")
public class TagController {

    @Autowired
    private TagService TagService;


    @GetMapping("/tags/{id}/input")
    public String editInput(@PathVariable Long id, Model model){
        model.addAttribute("tag",TagService.getTag(id));

        return "admin/tags-input";
    }

    @GetMapping("/tags")
    public String list(
               @RequestParam(required = false, defaultValue = "1") Integer current,  //必须是当前页，框架会自动转换
               @RequestParam(required = false, defaultValue = "3") Integer size,
               Model model){


        Page page = new Page(current,size);
        Page<Tag> tagPage = TagService.listTag(page);
        model.addAttribute("page",tagPage);
        return "admin/tags";
    }
    @GetMapping("/tags/input")
    public String input(Model model){

        model.addAttribute("tag",new Tag());
        return "admin/tags-input";
    }

    @PostMapping("/tags")
    public String TagsPost(@Valid Tag tag,//@Valid 为页面效验注解
                            BindingResult result, //校验接收的结果
                            RedirectAttributes attributes){


        Tag tag1 = TagService.getTagByName(tag.getName());
        if (tag1!=null){
            result.rejectValue("name","nameError","不能添加重复分类");
        }

        if(result.hasErrors()){
            return "admin/tags-input";
        }

        boolean t = TagService.saveTag(tag);
        if (t==false){
            attributes.addFlashAttribute("message","新增失败");

        }else {
            attributes.addFlashAttribute("message","新增成功");

        }
        return "redirect:/admin/tags";


    }


    @PostMapping("/tags/{id}")
    public String editPost(@Valid Tag tag,//@Valid 为页面效验注解
                            BindingResult result, //校验接收的结果
                            @PathVariable Long id,
                            RedirectAttributes attributes){


        Tag tag1 = TagService.getTagByName(tag.getName());
        if (tag1!=null){
            result.rejectValue("name","nameError","不能添加重复分类");
        }

        if(result.hasErrors()){
            return "admin/tags-input";
        }

        boolean t = TagService.updateTag(id,tag);
        if (t==false){
            attributes.addFlashAttribute("message","更新失败");

        }else {
            attributes.addFlashAttribute("message","更新成功");

        }
        return "redirect:/admin/tags";


    }

    @GetMapping("/tags/{id}/{current}/delete")
    public String delete(@PathVariable Long id,@PathVariable Integer current, RedirectAttributes attributes){

        TagService.deleteTag(id);

        attributes.addFlashAttribute("message","删除成功");
        //给跳转的页面加入参数
        attributes.addAttribute("current",current);
        return "redirect:/admin/tags";
    }




}
